package com.pulse.common.utils.sor;


import com.pulse.common.utils.sor.domain.Block;
import com.pulse.common.utils.sor.domain.Blocks;
import com.pulse.common.utils.sor.domain.Mapblock;
import com.pulse.common.utils.sor.domain.SorInfo;

import java.io.BufferedInputStream;
import java.util.ArrayList;

public class MapBlock {
    public static String process(BufferedInputStream fh, SorInfo sorInfo) {
        try{
//            // 使用 BufferedInputStream 包装 InputStream
//            // 以便支持 mark 和 reset 操作
//            if (!fh.markSupported()) {
//                fh = new java.io.BufferedInputStream(fh);
//            }

            // 标记当前位置，以便后续重置
            fh.mark(Integer.MAX_VALUE);

            // 读取文件开头的字符串
            String tt = Parts.getString(fh);
            if("Map".equals(tt)){
                sorInfo.setFormat(2);
            }
            else{
                sorInfo.setFormat(1);
                // 重置流到开头
                fh.reset();
            }

            // 获取版本号
            int versionInt = (int)Parts.getUInt(fh,2);
            sorInfo.setVersion(String.format("%.2f",versionInt*0.01));

            // 获取 Map 块的字节数
            Mapblock mapblock = sorInfo.getMapblock();
            if(mapblock==null){
                mapblock = new Mapblock();
            }
            mapblock.setNbytes((int)Parts.getUInt(fh,4));

            // 获取块的数量（不包括 Map 块）
            int nblocks = (int)Parts.getUInt(fh,2)-1;
            mapblock.setNblocks(nblocks);


            // 获取块信息
            Blocks blocks = sorInfo.getBlocks();
            if(blocks == null){
                blocks = new Blocks();
            }
            blocks.setOthers(new ArrayList<>());

            long startpos = (long)mapblock.getNbytes();

            for(int i = 0; i < nblocks; i++){
                String bname = Parts.getString(fh);
                int bverInt = (int) Parts.getUInt(fh,2);
                String bver = String.format("%.2f",bverInt * 0.01);
                long bsize = Parts.getUInt(fh,4);

                Block refBlock = new Block();
                refBlock.setName(bname);
                refBlock.setVersion(bver);
                refBlock.setSize((int)bsize);
                refBlock.setPos((int)startpos);
                refBlock.setOrder(i);

                switch (bname){
                    case "GenParams":
                        blocks.setGenParams(refBlock);
                        break;
                    case "SupParams":
                        blocks.setSupParams(refBlock);
                        break;
                    case "FxdParams":
                        blocks.setFxdParams(refBlock);
                        break;
                    case "KeyEvents":
                        blocks.setKeyEvents(refBlock);
                        break;
                    case "DataPts":
                        blocks.setDataPts(refBlock);
                        break;
                    case "Cksum":
                        blocks.setCksum(refBlock);
                        break;
                    default:
                        blocks.getOthers().add(refBlock);
                        break;

                }

                // 下一个块的起始位置
                startpos += bsize;
            }
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "nok";
        }
    }
}
